/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.client.gwt.ui;

import com.gadglet.client.gwt.GadgetConfig;
import com.gadglet.client.gwt.GadgetNativeUtils;
import com.gadglet.client.gwt.GadgetUtils;
import com.gadglet.client.gwt.core.ui.DomainFriendsDialogPanel;
import com.gadglet.client.gwt.core.ui.FriendsDialogPanel;
import com.gadglet.client.gwt.core.ui.ProfileEditDialogPanel;
import com.gadglet.params.GadgetType;
import com.gadglet.params.SharedConstants;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.PreferencesFeature;
import com.google.gwt.gadgets.client.PreferencesProvider;
import com.google.gwt.gadgets.client.impl.PreferencesUtil;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Gadget basic UI window
 * 
 * The Gadget is designed to have a header, footer, and main content area. the main content
 *  area is designed to enable scrolling when needed. The header is designed to have 3 links: help link,
 *  profile edit, friends dialogBox.
 *
 */
public class ViewMainPanel {
	protected VerticalPanel gadget = new VerticalPanel();
	protected HorizontalPanel header = new HorizontalPanel();
	protected ScrollPanel main = new ScrollPanel();
	protected VerticalPanel mainInner = new VerticalPanel();
	protected HorizontalPanel footer = new HorizontalPanel();
	protected final ProfileEditDialogPanel profileRegistration = ProfileEditDialogPanel
			.getProfileRegistration();
	protected  FriendsDialogPanel friendsPanel ;
	
	protected final DomainFriendsDialogPanel domainFriendsPanel = DomainFriendsDialogPanel
	.getFriendsPanel();
	
	

	public ViewMainPanel(boolean isCanvas) {

		int height = 0;
		
		if (isCanvas) {
			// 
			RootPanel.get();

		// adjust Height for Gmail Canvas View
			try {
				 height = GadgetNativeUtils.getGadgetHeight();

				if (height < 500){
					height = 600;
					GadgetNativeUtils.adjustHeight(height);
					gadget.setHeight(height + "px");
				}
			} catch (Exception e) {
				// do nothing
			}
		}

		// TODO: need to implement gadget solution
		// __BIDI_START_EDGE__: The value is "left" when the gadget is in
		// LTR-mode and "right" when the gadget is in RTL-mode.
		// __BIDI_END_EDGE__: The value is "right" when the gadget is in
		// LTR-mode and "left" when the gadget is in RTL-mode.
		// __BIDI_DIR__: The value of this variable is "ltr" when the gadget is
		// in LTR-mode and "rtl" when the gadget is in RTL-mode.
		// __BIDI_REVERSE_DIR__
		gadget.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LOCALE_START);
		// set size accordig to the screen
		int width = 0;
		
		try {
			width = GadgetNativeUtils.getGadgetWidth();
			if(height == 0)
				height = GadgetNativeUtils.getGadgetHeight();

		} catch (Exception e) {
			// ignore
		}
		if (width > 0 && height > 0) {
			main.setPixelSize(width, height - 50);
			gadget.setWidth(width + "px");
			gadget.setHeight(height + "px");
		}
		
		friendsPanel = FriendsDialogPanel.getFriendsPanel();
		
		main.add(mainInner);
		gadget.add(header);
		gadget.add(main);
		gadget.add(footer);

		// make header

		String helpIconUrl = GadgetUtils.getIconURL(null, "help.jpg", false);
		String href = GadgetUtils.getGadgetserver(false)
				+ SharedConstants.gadgetHelpDirName + "?gadget="
				+ GadgetConfig.getGadgetConfig().getGadgetName();
		PreferencesFeature prefsUtils = PreferencesProvider.get();

		Anchor help = new Anchor();
		help.setHref(href);
		help.setTitle(prefsUtils.getMsg("gadgetLabelHelp"));
		help.setTarget("_blank");
		help.setHTML("<img src=\"" + helpIconUrl + "\">");
		header.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LOCALE_START);
		header.add(help);
		header.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LOCALE_END);

		header.add(new HTML("&nbsp;"));

		// add profile link
		if (!GadgetConfig.getGadgetConfig().getGadgetType()
				.equals(GadgetType.OTHER)) {
			Image profileImage = new Image(GadgetUtils.getIconURL("profile",
					null, false));
			profileImage.setTitle(prefsUtils.getMsg("gadgetLabelProfile"));
			profileImage.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					profileRegistration.editProfile();

				}
			});

			profileImage.setStyleName("actionIcon");

			header.add(profileImage);
		}

		header.add(new HTML("&nbsp;"));

		// add friends link
		if (GadgetConfig.getGadgetConfig().getGadgetType()
				.equals(GadgetType.MINGLET)) {

			Image friendsImage = new Image(GadgetUtils.getIconURL("friends",
					null, false));
			friendsImage.setTitle(prefsUtils.getMsg("gadgetLabelFriends"));
			friendsImage.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					friendsPanel.showDialog();

				}
			});
			friendsImage.setStyleName("actionIcon");
			header.add(friendsImage);
		}
		else if (GadgetConfig.getGadgetConfig().getGadgetType()
				.equals(GadgetType.BIZLET)) {

			Image friendsImage = new Image(GadgetUtils.getIconURL("friends",
					null, false));
			friendsImage.setTitle(prefsUtils.getMsg("gadgetLabelFriends"));
			friendsImage.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					domainFriendsPanel.showDialog();

				}
			});
			friendsImage.setStyleName("actionIcon");
			header.add(friendsImage);
		}
		
		
		// make footer

		if (!GadgetUtils.isDefaultServer())
			footer.add(new HTML(prefsUtils.getMsg("gadgetMsgPoweredBy")));
		else {
			footer.add(new HTML(prefsUtils.getMsg("gadgetMsgCopyright")));
			Anchor contact = new Anchor();
			contact.setHref(GadgetUtils.getGadgetserver(false)
					+ SharedConstants.gadgetContactUs);
			contact.setTarget("_blank");
			contact.setHTML(prefsUtils.getMsg("gadgetLabelContactUs"));
			footer.add(new HTML("&nbsp;"));
			footer.add(contact);
		}

	}

	
	/**
	 * @param item Widget you want to add
	 */
	public void add(Widget item) {
		mainInner.add(item);
	}

	
	/**
	 * @return verticlPanel used to hold content 
	 */
	public VerticalPanel getMainPanel() {
		return mainInner;
	}

	
	/**
	 * @return
	 */
	public VerticalPanel getGadgetPanel() {
		return gadget;
	}

	
	/**
	 * @return the Gadget header
	 */
	public HorizontalPanel getHeader() {
		return this.header;
	}
	
	/**
	 * @return the Gadget footer
	 */
	public HorizontalPanel getFooter() {
		return this.footer;
	}

}
